package classes;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
	private static String DRVIER = "com.mysql.cj.jdbc.Driver"; // MySQL 椹卞姩
	private static String URL = "jdbc:mysql://127.0.0.1:3306/term_project?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; // URL
    private static String USERNAMR = "root"; // Oracle鐢ㄦ埛鍚�
    private static String PASSWORD = "1215"; // 瀵嗙爜

    public static Connection GetConnection(){
    	Connection conn = null;
    	try{
    		// 杩欓噷浣跨敤Class.forName() 鏂规硶鍒涘缓椹卞姩绋嬪簭鐨勫疄渚嬪苟涓旇嚜鍔ㄨ皟鐢―riverManager 瀵瑰叾娉ㄥ唽
    		Class.forName(DRVIER);
    		// 閫氳繃DriverManager 鑾峰彇鏁版嵁搴撹繛鎺�
    		conn = DriverManager.getConnection(URL, USERNAMR, PASSWORD);
    	} catch(SQLException e){
    		e.printStackTrace();
    	}
    	catch (ClassNotFoundException e) {
			e.printStackTrace();
			}
    	return conn;
    }
    
    /**
     * 鍏抽棴杩炴帴
     * @param conn
     */
    public static void close(Connection conn){
    	try{
    		if(conn != null && !conn.isClosed() )
    			conn.close();
    	} catch(SQLException e){
    		e.printStackTrace();
    	}
    }
    
    /**
     * 寰楀埌PreparedStatement 瀵硅薄
     * @param conn
     * @param sql
     * @return
     */
    public static PreparedStatement getStatement(Connection conn, String strsql){
    	if(strsql == null || "".equals(strsql) ){
    		System.out.println("SQL 涓虹┖鈥︹��");
    		return null;
    	}
    	if(conn == null){
    		System.out.println("杩炴帴涓虹┖鈥︹��");
    		return null;
    	}
    	
    	try{
    		return conn.prepareStatement(strsql,  // 棰勭紪璇戣鍙ュ緱鍒癙reparedStatement 瀵硅薄
    				ResultSet.TYPE_SCROLL_INSENSITIVE,
    				ResultSet.CONCUR_UPDATABLE);
    	} catch(SQLException e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
    /**
     * 寰楀埌 ResultSet
     * @param pstmt
     * @return
     */
    public static ResultSet executeQuery(PreparedStatement pstmt){
    	try{
    		if(pstmt!=null)
    			return pstmt.executeQuery(); // 鏌ヨ
    	} catch(SQLException e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
    /**
     * 鎵ц澧炪�佸垹銆佹敼鐨勬搷浣�
     * @param sql
     * @return
     */
    public static int executeUpdate(String sql){
    	int returnvalue = 0;
    	Connection conn = DB.GetConnection();
    	try{
    		Statement stmt = conn.createStatement();
    		returnvalue = stmt.executeUpdate(sql);
    		return returnvalue;
    	} catch(SQLException e){
    		System.out.println(e.getMessage());
    		return -1;
    	}
    }
    
    /**
     * 鍏抽棴Statement 瀵硅薄
     * @param stmt
     */
    public static void close(Statement stmt){
    	try{
    		if(stmt!=null) {
    			stmt.close();
    		}
    	} catch(SQLException e){
    		e.printStackTrace();
    	}
    }
    
    /**
     * 鍏抽棴缁撴灉闆�
     * @param rs
     */
    public static void close(ResultSet rs){
    	try{
    		if(rs!=null){
    			rs.close();
    		}
    	} catch(SQLException e){
			e.printStackTrace();
		}
    }
    
    
    
    
    public static void main(String[] args){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs= null;
        try{
        	
        	conn=DB.GetConnection();
        	stmt=conn.createStatement();
        	 String sql = "select * from student where sno="+"'20200273'";
             rs = stmt.executeQuery(sql);// 杩斿洖鏌ヨ缁撴灉
             if(rs.next()) {
            	 System.out.println("鏈�");
             }
             else {
            	 System.out.println("鏃�");
             }
//             瀹屾垚鍚庡叧闂�
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 澶勭悊 JDBC 閿欒
            se.printStackTrace();
        }catch(Exception e){
            // 澶勭悊 Class.forName 閿欒
            e.printStackTrace();
//          }
        }
       }
}
